package com.kelaite.procurement.service.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kelaite.procurement.entity.TCity;
import com.kelaite.procurement.entity.TProvince;
import com.kelaite.procurement.repository.CityRepository;
import com.kelaite.procurement.service.CityService;

@Service
@Transactional
public class CityServiceImpl implements CityService{
  
	@Autowired
	private CityRepository cityRepository; 
	
	@PersistenceContext
	private EntityManager em;
	
	@Override
	public TCity save(TCity persistence) {
		return this.cityRepository.save(persistence);
	}

	@Override
	public TCity getObject(Long id) {
		return this.cityRepository.findOne(id);
	}

	@Override
	public List<TCity> getAll() {
		return this.cityRepository.findAll();
	}

	@Override
	public void deleteByObject(TCity persistence) {
		this.cityRepository.delete(persistence);
	}

	@Override
	public void deleteByPrimaryKey(Long id) {
		this.cityRepository.delete(id);
	}
	
	@SuppressWarnings({ "unchecked", "deprecation" })
	@Override
	public List<TCity> getByProperty(String property, Object value) {
		Session session = (org.hibernate.Session) em.getDelegate();
		Criteria criteria = session.createCriteria(TCity.class).add(org.hibernate.criterion.Expression.eq(property, value));
		return criteria.list();
	}

	@Override
	public List<TCity> findByTProvince(TProvince tProvince) {
		// TODO Auto-generated method stub
		return cityRepository.findByTProvince(tProvince);
	}
	
	/*@Override
	public List<TCity> findByCityName(String cityName) {
		Session session = (org.hibernate.Session) em.getDelegate();
		//TypedQuery query = em.createQuery("select a from t_city a where a.cityName = :cityName", TCity.class);
		//query.setParameter("cityName", cityName);
		//CriteriaBuilder cb = em.getCriteriaBuilder();
		//CriteriaQuery query = cb.createQuery(TCity.class).
		Criteria criteria = session.createCriteria(TCity.class).add(org.hibernate.criterion.Expression.eqOrIsNull("cityName", cityName));
		return criteria.list();
		//return query.getResultList();
	}*/
}
